# @Version : 1.0
# @Author  : wxz
# @File    : page_helper.py
# @Time    : 2025/5/31 20:58
# @Describe: 描述...
from typing import Any


class PageHelper:

    # 当前第几页,系统默认
    page_no = 1
    # 每页多少条，系统默认
    page_size = 10

    @staticmethod
    def paginate(page_no, page_size, total_count)-> dict[str, Any]:
        """
        分页校验、封装
        :param page_no:  当前第几页
        :param page_size: 每页多少条
        :param total_count: 一共有多少条记录
        :return:
        """
        if total_count <= 0:
            return {
                "page_no": page_no,
                "page_size": page_size,
                "offset": 0,
                "total_page": 0,
            }
        if page_no < 1 or page_no > total_count:
            page_no = 1
        if page_size > 100 or page_size < 1:
            page_size = 10

        # 一共有多少页
        total_page = int(total_count / page_size) + 1
        if page_no > total_page:
            page_no = total_page
        # 计算偏移量
        offset = (page_no - 1) * page_size

        return {
            "page_no": page_no,
            "page_size": page_size,
            "offset": offset,
            "total_page": total_page,
        }



